package cn.wolfcode.service.impl;

import cn.wolfcode.domain.ActivityItem;
import cn.wolfcode.mapper.ActivityItemMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IActivityItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ActivityItemServiceImpl implements IActivityItemService {

    @Autowired
    private ActivityItemMapper activityItemMapper;


    @Override
    public void save(ActivityItem activityItem) {
        activityItemMapper.insert(activityItem);
    }

    @Override
    public void delete(Long id) {
        activityItemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Long radioId) {
        activityItemMapper.updateById(radioId);
    }

    @Override
    public ActivityItem get(Long id) {
        return activityItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ActivityItem> listAll() {
        return activityItemMapper.selectAll();
    }

    @Override
    public PageInfo<ActivityItem> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<ActivityItem> activityItems = activityItemMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<ActivityItem>(activityItems);
    }
    //通过投票活动id查询到对应的选项
    @Override
    public List<ActivityItem> selectByAid(Long id) {
        List<ActivityItem> activityItems = activityItemMapper.selectByAid(id);
        //遍历对应的选项
        Integer totalNum = 0;//初始化总的投票人数
        for (int i = 0; i < activityItems.size(); i++) {
            ActivityItem item = activityItems.get(i);
            Integer num = item.getNum();
            totalNum += num;
        }
        for (ActivityItem activityItem:activityItems) {
            activityItem.setTotalNum(totalNum);
        }

        return activityItems;
    }
}
